<?php

namespace App\Admin\Actions\Tools;

use Encore\Admin\Grid\Tools\AbstractTool;
use App\Models\Cate;

class CateSelect extends AbstractTool
{
    public function __construct($app)
    {
        $this->app = $app;
    } 
	public function rooturl()
    {
		return $this->grid->resource();
	}

	public function render()
    {
		$app = get_app($this->app);
		if( empty($app['has_cate_sets']) ) return '';
		
		$cate_id = request('cate_id',0);
		$cates = Cate::selectOptions(function ($query) use($app){
			return $query->where('app_name', $app['name']);
		}, '全部');
		$sel = '<div class="input-group input-group-sm pull-right" style="max-width:250px;margin-left:6px;">
					<span class="input-group-btn">
                     <button class="btn btn-primary">分类选择</button>
				</span>';
		$sel .= "<select name='cate_id' onchange=\"window.location.href='{$this->rooturl()}?cate_id='+this.options[this.options.selectedIndex].value\" class='form-control'>";
		foreach($cates as $key => $title){
				$selected =  ($key==$cate_id) ? "selected" : "";
				$sel .= "<option value='{$key}' {$selected}>{$title}</option>";
		}
		$sel .= '</select>
			</div>';	
		return $sel;
    }
}